package com.guigu.ssyx.product.controller;


import com.atguigu.ssyx.vo.product.CategoryQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.ssyx.common.result.Result;
import com.guigu.ssyx.product.entity.Category;
import com.guigu.ssyx.product.service.CategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author liuyang
 * @since 2025-09-10
 */
@Api(value="商品分类接口", tags={"商品分类接口"})
@RestController
@RequestMapping("/admin/product/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit, CategoryQueryVo categoryQueryVo) {
        IPage<Category> categoryPage = new Page<>(page, limit);

        IPage<Category> list =  categoryService.selectPageCategory(categoryPage, categoryQueryVo);

        return Result.ok(list);

    }

    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
         Category category = categoryService.getById(id);
         return Result.ok(category);
    }

    @PostMapping("save")
    public Result save(@RequestBody Category category){
        categoryService.save(category);
        return Result.ok(null);
    }

    @PutMapping("update")
    public Result update(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.ok(null);
    }

    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        categoryService.removeById(id);
        return Result.ok(null);
    }

    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        categoryService.removeByIds(idList);
        return Result.ok(null);
    }

    @GetMapping("findAllList")
    public Result findAllList(){
        List<Category> list = categoryService.list();
        return Result.ok(list);
    }


}

